package ru.royalgatemagazine.app.show.main;

import java.util.List;

import ru.royalgatemagazine.app.Debug;
import ru.royalgatemagazine.app.DialogGenerator;
import ru.royalgatemagazine.app.DialogGenerator.DialogGeneratorListener;
import ru.royalgatemagazine.app.R;
import ru.royalgatemagazine.app.json.JsonApiReader;
import ru.royalgatemagazine.app.json.JsonApiReader.JsonApiReaderListener;
import ru.royalgatemagazine.app.show.about.AboutActivity;
import ru.royalgatemagazine.app.show.page.PageActivity;
import android.app.Dialog;
import android.app.ListActivity;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;

public class MainCatalogActivity extends ListActivity implements JsonApiReaderListener, MainCatalogController, DialogGeneratorListener {
	
	private Dialog mDialog;
	
	private void dismissDialog(){
		try{
			if(mDialog!=null && mDialog.isShowing()){
				mDialog.dismiss();
			}
		}
		catch(Exception e){
			Debug.log("try to dismiss dialog" + e.toString());
		}
	}
	
	private void prepTitle(){
		findViewById(R.id.button1).setVisibility(View.GONE);
		findViewById(R.id.button2).setVisibility(View.GONE);
		TextView v = (TextView) findViewById(R.id.text);
		v.setText(R.string.king_towers);
		findViewById(R.id.pic).setOnClickListener(onPicClicker);
	}
	
	public OnClickListener onPicClicker = new OnClickListener() {
		@Override
		public void onClick(View v) {
			Intent i = new Intent(MainCatalogActivity.this, AboutActivity.class);
			startActivity(i);
		}
	};
	
	 @Override
	    public void onCreate(Bundle savedInstanceState) {
	        super.onCreate(savedInstanceState);
	        setContentView(R.layout.main);
	        MainCatalogAdapter adapter = new MainCatalogAdapter(this, this);
	        getListView().setCacheColorHint(Color.parseColor("#222222"));
	        getListView().setAdapter(adapter);
	        prepTitle();
	    }
	 
	@Override
	protected void onResume() {
		super.onResume();
		String text = getString(R.string.loading);
		mDialog = DialogGenerator.createProgressTitledDialog(
				this, this, DialogGenerator.DIALOG_PROGRESS, null, text);
		mDialog.show();
		JsonApiReader.getMagazineList(this, this, new Handler());
	} 
	 
	@Override
	public void onMagazineListLoaded(List<MainEntity> list) {
		Debug.log("Activity:");
		Debug.log("onMagazineListLoaded:");
		for(MainEntity m : list){
			Debug.log("Activity:"+m);
		}
		((MainCatalogAdapter)getListView().getAdapter()).refresh(list);
		dismissDialog();
	}

	@Override
	public void onError(int erorCode) {
		dismissDialog();
		String text = getString(R.string.service_unavailable);
		String ok = getString(R.string.ok);
		mDialog = DialogGenerator.createOneButtonDialog(
				this, this, DialogGenerator.DIALOG_NOTIFICATION, 
				null, text, ok);
		Debug.log("Activity:");
		Debug.log("onError"+erorCode);
	}

	@Override
	public void onOpenClick(MainEntity entity) {
		Debug.log("Activity:onOpenClick()");
		if(entity!=null && entity.pages>0){
			Intent intent = new Intent(this, PageActivity.class);
			intent.putExtra(PageActivity.INTENT_EXTRA_MID, entity.mid);
			intent.putExtra(PageActivity.INTENT_EXTRA_PAGE, 1);
			intent.putExtra(PageActivity.INTENT_EXTRA_PAGES, entity.pages);
			startActivity(intent);
		}
		else{
			Debug.log("PAGES==0, Dont start Intent");
		}
	}

	@Override
	public void onClick(DialogInterface dialog, int which) {
		dismissDialog();
	}

	@Override
	public void onCancel(DialogInterface dialog) {
		dismissDialog();
	}
}
